package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.GroupSale;
import com.ericom.service.GenericManager;

public class GroupSaleAction extends BaseAction {

	private GenericManager<GroupSale, Long> groupSaleManager;
	private List groupSales;
	private GroupSale groupSale;
	private Long id;

	public GroupSale getGroupSale() {
		return groupSale;
	}

	public void setGroupSale(GroupSale groupSale) {
		this.groupSale = groupSale;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setGroupSaleManager(
			GenericManager<GroupSale, Long> groupSaleManager) {
		this.groupSaleManager = groupSaleManager;
	}

	public String list() {
		groupSales = groupSaleManager.getAll();
		return SUCCESS;
	}

	public List getGroupSales() {
		return groupSales;
	}

	public String delete() {
		groupSaleManager.remove(groupSale.getId());
		this.saveMessage(getText("groupSale.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			groupSale = groupSaleManager.get(id);
		} else {
			groupSale = new GroupSale();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (groupSale.getId() == null);
		groupSale = groupSaleManager.save(groupSale);

		String key = (isNew) ? "groupSale.added" : "groupSale.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
